import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 全屏遮罩更新弹窗
/// 返回值：确认返回 true，取消返回 false/null。onDontRemindChanged 可用于持久化“此版本不再提醒”状态。
Future<bool?> showUpdateDialog({
  bool? isForceUpdate,
  required BuildContext context,
  required String content,
  String title = '有新版本更新',
  String confirmText = '立即更新',
  String cancelText = '取消',
  bool initialDontRemind = false,
  ValueChanged<bool>? onDontRemindChanged,
  VoidCallback? onConfirm,
  VoidCallback? onCancel,
}) {
  bool dontRemind = initialDontRemind;

  return showGeneralDialog<bool>(
    context: context,
    barrierDismissible: false,
    barrierLabel: 'update_dialog',
    barrierColor: Colors.black54, // 全屏遮罩
    pageBuilder: (_, __, ___) => const SizedBox.shrink(),
    transitionBuilder: (ctx, anim, __, ___) {
      final curved = Curves.easeOutBack.transform(anim.value);
      return Opacity(
        opacity: anim.value,
        child: Transform.scale(
          scale: 0.9 + 0.1 * curved,
          child: Center(
            child: Material(
              color: Colors.transparent,
              child: StatefulBuilder(
                builder: (context, setState) {
                  return Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      // 白色弹框
                      ClipRRect(
                        borderRadius: BorderRadius.circular(16),
                        child: Container(
                          width: 300.w,
                          color: Colors.white,
                          padding: EdgeInsets.symmetric(
                            horizontal: 20.w,
                            vertical: 18.h,
                          ),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Center(
                                child: Text(
                                  title,
                                  textAlign: TextAlign.center,
                                  style: context.textTheme.titleLarge!.copyWith(
                                    color: context
                                        .customColorThemeNonNull
                                        .textColor1,
                                  ),
                                ),
                              ),
                              SizedBox(height: 12.h),
                              Text(
                                content,
                                textAlign: TextAlign.left,
                                style: context.textTheme.headlineMedium!
                                    .copyWith(
                                      color: context
                                          .customColorThemeNonNull
                                          .textColor3,
                                    ),
                              ),
                              SizedBox(height: 18.h),
                              Divider(height: 1.h, color: Color(0xFFE5E5E5)),
                              IntrinsicHeight(
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: TextButton(
                                        onPressed: () {
                                          onCancel?.call();
                                          Navigator.pop(context, false);
                                        },
                                        child: Text(
                                          cancelText,
                                          style: context
                                              .textTheme
                                              .headlineLarge!
                                              .copyWith(
                                                color: context
                                                    .customColorThemeNonNull
                                                    .textColor1,
                                              ),
                                        ),
                                      ),
                                    ),
                                    Container(
                                      width: 1,
                                      color: const Color(0xFFE5E5E5),
                                    ),
                                    Expanded(
                                      child: TextButton(
                                        onPressed: () {
                                          if (onConfirm != null) {
                                            onConfirm.call();
                                          } else {
                                            Navigator.pop(context, true);
                                          }
                                        },
                                        child: Text(
                                          confirmText,
                                          style: context
                                              .textTheme
                                              .headlineLarge!
                                              .copyWith(
                                                color:
                                                    context.colorScheme.primary,
                                              ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                      if (isForceUpdate != null && isForceUpdate == false)
                        Container(
                          width: 300.w,
                          padding: EdgeInsets.only(top: 10.h, bottom: 8.h),
                          color: Colors.transparent,
                          child: InkWell(
                            onTap: () {
                              dontRemind = !dontRemind;
                              onDontRemindChanged?.call(dontRemind);
                              setState(() {});
                            },
                            child: Row(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Checkbox(
                                  value: dontRemind,
                                  onChanged: (val) {
                                    dontRemind = val ?? false;
                                    onDontRemindChanged?.call(dontRemind);
                                    setState(() {});
                                  },
                                  materialTapTargetSize:
                                      MaterialTapTargetSize.shrinkWrap,
                                  visualDensity: VisualDensity.compact,
                                  activeColor: Colors.white,
                                  checkColor: context.colorScheme.primary,
                                  side: const BorderSide(color: Colors.white),
                                  fillColor: MaterialStateProperty.resolveWith(
                                    (states) => Colors.white,
                                  ),
                                ),
                                SizedBox(width: 4.w),
      
                                  Text(
                                    '此版本不再提醒',
                                    style: context.textTheme.bodyMedium!.copyWith(
                                      color: Colors.white,
                                    ),
                                  ),
                              ],
                            ),
                          ),
                        ),
                    ],
                  );
                },
              ),
            ),
          ),
        ),
      );
    },
  );
}
